package com.xana.acg.mikomiko.frags.anime;

import android.text.Html;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.xana.acg.com.app.PresenterFragment;
import com.xana.acg.com.view.recycler.RecyclerAdapter;
import com.xana.acg.fac.model.anime.Detail;
import com.xana.acg.fac.presenter.play.AnimePlayerContract;
import com.xana.acg.fac.presenter.play.AnimePlayerPresenter;
import com.xana.acg.com.utils.TextUtils;
import com.xana.acg.mikomiko.R;
import com.xana.acg.mikomiko.actis.anime.AnimePlayerActivity;

import java.util.List;

import butterknife.BindView;

public class SrcFragment extends PresenterFragment<AnimePlayerContract.Presenter>
        implements AnimePlayerContract.View,
        AnimePlayerActivity.OnFragListener {

    @BindView(R.id.tv_title)
    TextView mTitle;
    @BindView(R.id.tv_des)
    TextView mDes;
    @BindView(R.id.recycler)
    RecyclerView mRv;
    private SrcAapter srcAapter;

    private AnimePlayerActivity mAct;

    private String uri;
    private boolean first = true;
    private EpiAdapter.EpiViewHolder curEpisode;
    private EpiAdapter mAapter;


    @Override
    protected void onFirstInit() {
        super.onFirstInit();
        mAct = ((AnimePlayerActivity) a());
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_detail;
    }

    @Override
    protected void initWidget(View root) {
        super.initWidget(root);
        if(uri==null) return;
        mRv.setLayoutManager(new LinearLayoutManager(getContext()));
        mRv.setAdapter(srcAapter = new SrcAapter());
        mRv.setNestedScrollingEnabled(false);
        mPresenter.get(uri);
    }

    @Override
    protected AnimePlayerContract.Presenter initPresenter() {
        return new AnimePlayerPresenter(this);
    }

    @Override
    public void onLoad(Detail detail) {

        ok(0);
        mTitle.setText(detail.getTitle());
        // darling in the franxx
        mDes.setText(Html.fromHtml(detail.getDescription()));
        srcAapter.add(detail.getPlay_lists());
    }

    @Override
    public void onLoad(String uri) {
        this.uri = uri;
    }

    @Override
    public void play(int i) {
        mAapter.getListener().onItemClick((RecyclerAdapter.ViewHolder) rv.findViewHolderForAdapterPosition(i), mAapter.getItemList().get(i));
    }

    private RecyclerView rv;



    @Override
    public void next() {
        int p = curEpisode.getAdapterPosition();
        if(++p>=mAapter.getItemCount()) return;
        play(p);
    }

    class SrcAapter extends RecyclerAdapter<Detail.Src> {

        @Override
        protected int getItemViewType(int position, Detail.Src src) {
            return R.layout.item_anime_src;
        }

        @Override
        protected RecyclerAdapter.ViewHolder<Detail.Src> onCreateViewHolder(View view, int viewType) {
            return new SrcViewHolder(view);
        }

        class SrcViewHolder extends RecyclerAdapter.ViewHolder<Detail.Src> {

            @BindView(R.id.tv_title)
            TextView mTitle;

            @BindView(R.id.recycler)
            RecyclerView mRv;

            public SrcViewHolder(@NonNull View itemView) {
                super(itemView);
            }

            @Override
            protected void onBind(Detail.Src src) {
                mTitle.setText(String.format(getString(R.string.label_sel_src), src.getName(), src.getNum()));
                mRv.setLayoutManager(new GridLayoutManager(getContext(), 2));
                mRv.setAdapter(new EpiAdapter(src.getVideo_list(), new AdapterListener<Detail.Eposide>() {
                    @Override
                    public void onItemClick(ViewHolder holder, Detail.Eposide eposide) {
                        if(holder==curEpisode) return;
                        rv = mRv;
                        mAapter = (EpiAdapter) mRv.getAdapter();
                        mAct.onEpiClick(mAapter.getItemList(), holder.getAdapterPosition());
                        EpiAdapter.EpiViewHolder clickEposide = (EpiAdapter.EpiViewHolder) holder;
                        clickEposide.mTitle.setTextColor(getResources().getColor(R.color.colorAccent));
                        if(curEpisode!=null) {
                            curEpisode.mTitle.setTextColor(getResources().getColor(R.color.black));
                        }
                        curEpisode = clickEposide;
                    }

                    @Override
                    public void onItemLongClick(ViewHolder holder, Detail.Eposide eposide) {

                    }
                }));
            }
        }
    }

    class EpiAdapter extends RecyclerAdapter<Detail.Eposide> {

        public EpiAdapter(List<Detail.Eposide> eposideList, AdapterListener<Detail.Eposide> listener) {
            super(eposideList, listener);
        }

        @Override
        protected int getItemViewType(int position, Detail.Eposide eposide) {
            return position % 2 == 0 ? R.layout.item_episode_left : R.layout.item_episode_right;
        }

        @Override
        public void onBindViewHolder(ViewHolder<Detail.Eposide> holder, int position) {
            super.onBindViewHolder(holder, position);
            if(first&&position==0) {
                getListener().onItemClick(holder, mDataList.get(0));
                curEpisode = (EpiViewHolder) holder;
                mAapter = this;
                first = false;
            }
        }

        @Override
        protected ViewHolder<Detail.Eposide> onCreateViewHolder(View view, int viewType) {
            return new EpiViewHolder(view);
        }

        class EpiViewHolder extends ViewHolder<Detail.Eposide> {
            @BindView(R.id.tv_title)
            TextView mTitle;
            @BindView(R.id.fl_back)
            FrameLayout mBack;

            public EpiViewHolder(View itemView) {
                super(itemView);
            }

            @Override
            protected void onBind(Detail.Eposide eposide) {
                String name = eposide.getName();
                mTitle.setText(TextUtils.isNumeric(name)? String.format(getString(R.string.label_episode), name): name);
            }
        }
    }
}